package org.example.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.domain.User;
import org.example.mapper.UserMapper;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {



    @Autowired
    public Optional<User> selectByID(Long id) {
        User user = userMapper.selectByID(id);
        return Optional.ofNullable(user);
    }

    @Autowired
    public Optional<List<User>> selectAll() {
        return userMapper.selectAll();
    }

    @Autowired
    public Boolean insert(User user) {
        try {
            return userMapper.insert(user);
        } catch (DuplicateKeyException e) {
            System.out.println("Email has already existed: " + user.getEmail());
            return false;
        }
    }

    @Autowired
    public int deleteById(Long id) {
        return userMapper.deleteById(id);
    }

    @Autowired
    public List<User> selectByWrapper(String name) {
        return userMapper.selectByWrapper(name);
    }

    @Autowired
    public boolean updateUser(User user) {
        return userMapper.updateUser(user);
    }
}